package com.lzy.business.controller.approval;

import com.github.pagehelper.PageInfo;
import com.lzy.business.controller.BaseController;
import com.lzy.business.facade.CouponProfitRecordFacade;
import com.lzy.business.request.CouponProfitRecordRequest;
import com.lzy.business.response.CouponProfitRecordResponse;
import com.lzy.framework.component.PageBean;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DozerUtils;
import com.lzy.product.apis.CouponProfitRecordServiceApi;
import com.lzy.product.common.util.EnumUtil;
import com.lzy.product.constants.BusinessType;
import com.lzy.product.dtos.CouponProfitRecordDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * 卡券宝收益记录
 * Created by WIN7 on 2018/1/11.
 */
@Controller
@RequestMapping("/admin/coupon/profit")
public class CouponProfitRecordController extends BaseController {
    @Autowired
    private CouponProfitRecordFacade couponProfitRecordFacade;
    @Autowired
    private CouponProfitRecordServiceApi couponProfitRecordServiceApi;

    /**
     * 分利错误记录页面
     *
     * @return
     */
    @RequestMapping(value = "couponProfitListPage", method = RequestMethod.GET)
    public String couponProfitListPage(Model model) {
        List<BusinessType.CouponProfitStatus> statusList = EnumUtil.valueOfList(BusinessType.CouponProfitStatus.class);
        model.addAttribute("statusList", statusList);
        return "approval/coupon_profit_list";
    }

    /**
     * 分利错误记录列表   返回json在页面
     */
    @RequestMapping(value = "/json", method = RequestMethod.GET)
    @ResponseBody
    public PageBean couponProfitList(CouponProfitRecordRequest request) {
        Pager pager = new Pager();
        pager.setPageSize(request.getPageSize());
        pager.setPageNum(request.getPageNum());
        PageInfo<CouponProfitRecordResponse> pageInfo = couponProfitRecordFacade.couponProfitList(pager, DozerUtils.map(request, CouponProfitRecordDto.class));
        if (pageInfo != null) {
            return new PageBean(pageInfo.getTotal(), pageInfo.getList());
        }
        return null;
    }

    /**
     * 处理错误记录   返回json在页面
     */
    @RequestMapping(value = "/dealCouponProfit/{id}/{status}", method = RequestMethod.PUT)
    @ResponseBody
    public Result dealCouponProfit(@PathVariable("id") String id, @PathVariable("status") Integer status) {
        CouponProfitRecordDto dto = couponProfitRecordServiceApi.findById(Long.parseLong(id));

        List<CouponProfitRecordDto> list = new ArrayList<>();
        list.add(dto);

        Ensure.that(couponProfitRecordFacade.updateProfitRecordsStatus(list, status)).isTrue("13020005");
        return new Result();
    }
}
